<?php
require '../script/mysqlconfig.php';
$conn = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
if ($conn->connect_error) {
    die("连接数据库失败: " . $conn->connect_error);
}

//获取要编辑的数据
$id = $_GET["id"];
$result1 = mysqli_query($conn, "SELECT * FROM users WHERE id='$id' ");
$row = mysqli_fetch_assoc($result1);
//输出表单
echo "<form method='POST' enctype='multipart/form-data'>";
echo "<input type='hidden' name='id' value='".$row['id']."'>";
echo "<table border='1' style='width:1130px;border-collapse:collapse;'>";

echo "<td align='right'>用户名:<input type='text' name='name' value='".$row['name']."'></td>";
echo "<td align='right'>密码:<input type='text' name='password' value='".$row['password']."'></td>";
echo "<td align='right'>组:<input type='text' name='group' value='".$row['group']."'></td>";
echo "<td align='right'>角色:<input type='text' name='role' value='".$row['role']."'></td>";
echo "<td align='right'>备注:<input type='text' name='info' value='".$row['info']."'></td>";

echo "</table>";
echo "<input type='submit' value='保存更新'>";
echo "</form>";

//处理提交的表单
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $id = $_POST["id"];
    $name = $_POST["name"];
    $password = $_POST["password"];
    $group = $_POST["group"];
    $role = $_POST["role"];
    $info = $_POST["info"];
    $result = mysqli_query($conn, "UPDATE `users` SET `name` = '$name', `password`  = '$password', `group`  = '$group',  `role` = '$role',  `info` = '$info' WHERE id='$id' ");
    if ($result == true) {
        echo "设备信息更新成功";
    } else {
        echo "设备信息更新失败" . "<br>" . $conn->error;
    }
}

mysqli_close($conn);
